package com.txy.domain.strategy.service.raffle;

import com.txy.domain.strategy.model.entity.StrategyAwardEntity;
import com.txy.domain.strategy.model.vo.RuleTreeVO;
import com.txy.domain.strategy.model.vo.StrategyAwardRuleModelVo;
import com.txy.domain.strategy.model.vo.StrategyAwardStockVO;
import com.txy.domain.strategy.repository.IStrategyRepository;
import com.txy.domain.strategy.service.IRaffleAward;
import com.txy.domain.strategy.service.IRaffleStock;
import com.txy.domain.strategy.service.armory.IStrategyDispatch;
import com.txy.domain.strategy.service.rule.chain.ILogicChain;
import com.txy.domain.strategy.service.rule.chain.factory.LogicChainFactory;
import com.txy.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import com.txy.domain.strategy.service.rule.tree.factory.engine.IDecisionTreeEngine;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class DefaultRaffleServiceImpl extends AbstractRaffleService implements IRaffleStock, IRaffleAward {

    @Resource
    private LogicChainFactory logicChainFactory;

    @Resource
    private DefaultTreeFactory treeFactory;

    @Resource
    private IStrategyRepository repository;


    DefaultRaffleServiceImpl(IStrategyDispatch strategyDispatch, IStrategyRepository repository, LogicChainFactory logicFactory, DefaultTreeFactory defaultTreeFactory) {
        super(strategyDispatch, repository, logicFactory, defaultTreeFactory);
    }

    @Override
    protected LogicChainFactory.LogicChainRes logicChain(Long strategyId, String userId) {
        ILogicChain iLogicChain = logicChainFactory.openLogicChain(strategyId);
        // 责任链抽奖
        return iLogicChain.logic(strategyId, userId);
    }

    @Override
    protected DefaultTreeFactory.AwardResClass logicTree(Long strategyId, String userId, Integer awardId) {
        StrategyAwardRuleModelVo ruleModelVo = repository.queryRuleModelByStrategyIdAndAwardId(strategyId, awardId);
        RuleTreeVO ruleTreeVO = repository.queryRuleTreeByTreeId(ruleModelVo.getRuleModels());

        IDecisionTreeEngine iDecisionTreeEngine = treeFactory.openLoginRuleTree(ruleTreeVO);
        return iDecisionTreeEngine.process(strategyId, userId, awardId);

    }

    @Override
    public StrategyAwardStockVO getFromQueue() {
        return repository.getAwardQueue();
    }

    @Override
    public Boolean updateAwardStock(StrategyAwardStockVO strategyAwardStockVO) {
        return repository.updateAwardStock(strategyAwardStockVO);
    }

    @Override
    public List<StrategyAwardEntity> queryRaffleAwardList(Long strategyId) {
        return repository.queryStrategyAwardList(strategyId);
    }
}
